<?php

namespace App\Http\Controllers;


use App\Library\CaptchaCode;
use App\Models\Banners;
use App\Models\Baoming;
use App\Models\Music;
use App\Models\ProProducts;
use App\Models\WebContent;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{

    public function __construct()
    {


    }

    protected static function webView($tpl = '', $data = [])
    {
        $banners = Banners::all();

        $data = array_merge(['banners' => $banners], $data);

        return view($tpl, $data);
    }

    public function index()
    {

        $model = new WebContent();

        $content = $model->find(1);

        return self::webView('web.index', compact('content'));
    }

    public function rules()
    {
        $model = new WebContent();

        $content = $model->find(2);

        return self::webView('web.rules', compact('content'));
    }

    public function schedule()
    {
        $model = new WebContent();

        $content = $model->find(3);

        return self::webView('web.schedule', compact('content'));
    }

    public function data($id)
    {
        $model = new Music();
        $lists = $model->get()->toArray();

        $info = $model->find($id);

        $music = explode('|', $info['lists']);


        return self::webView('web.music', compact('lists', 'info', 'music'));
    }

    public function news()
    {

        $model = new ProProducts();
        $list = $model->where('categoryid', 1)->orderBy('id', 'desc')->get()->toArray();

        return self::webView('web.news', compact('list'));
    }

    public function newsDetail($id)
    {
        $model = new ProProducts();
        $content = $model->find($id);
        return self::webView('web.newsDetail', compact('content'));
    }


    public function problem()
    {
        $model = new WebContent();

        $content = $model->find(4);

        return self::webView('web.problem', compact('content'));
    }

    public function about()
    {
        return self::webView('web.about');
    }


    public function sign()
    {
        return view('h5.sign');
    }

    public function register()
    {
        $params = Input::all();

//        if (!$this->checkcaptcha($params['verify'])) {
//            $this->error('验证码错误，请点击刷新验证码重试');
//        }

        $rules = [
            'pic' => 'required|image|max:1024',
            'email' => 'email',
        ];

        $messages = [
            'pic.required' => '请上传照片',
            'pic.image' => '必须上传图片',
            'pic.max' => '上传图片大小不得超过1MB',
        ];

        $this->validator($params, $rules, $messages);

        $saveContent = array(
            '赛区' => $params['saiqu'],
            '姓名' => $params['relname'],
            '性别' => $params['sex'],
            '民族' => $params['minzu'],
            '年龄' => $params['age'],
            '城市' => $params['city'],
            '学校' => $params['school'],
            '指导老师' => $params['teacher'],
            '参赛编号' => $params['bsId'],
            '参赛人数' => $params['bsCount'],
            '参赛形式' => $params['bsType'],
            '伴奏形式' => $params['banzou'],
            '参赛乐器' => $params['yueqi'],
            '品牌' => $params['pingpai'],
            '型号' => $params['yueqiType'],
            '参赛曲目' => $params['song'],
            '联系电话' => $params['tel'],
            '联系地址' => $params['address'],
            '邮箱地址' => $params['email'],
        );

        $model = new Baoming();
        if (isset($params['pic'])) {
            $file = $params['pic'];
            if ($file->isValid()) {
                $clientName = $file->getClientOriginalName();
                $tmpName = $file->getFileName();
                $extension = $file->getClientOriginalExtension(); //上传文件的后缀.
                $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
                $path = $file->move('upload/image', $newName);
                $model->pic = 'image/' . $newName;
            }
        }

        $model->relname = $params['relname'];
        $model->content = $saveContent;

        $model->save();

        $this->success('报名成功');
    }

    public function clause()
    {
        return view('h5.tiaokuan');
    }

    //验证码
    public function captcha($tmp)
    {
        $captcha = new CaptchaCode(4);
        $code = $captcha->getCode();

        Session::flash('captcha', $code);
        ob_start();

        $captcha->outPut();
        return response(ob_get_clean(), 200, array('content-type' => 'image/png'));
    }

    //验证验证码
    private function checkcaptcha($captcha)
    {

        if ($captcha != Session::get('captcha')) {
            return false;
        }

        return true;
    }

    protected function error($msg = '未知错误')
    {
        echo "<script>
alert('{$msg}');
history.go(-1);
</script>";
        exit ();
    }

    protected function success($msg = '操作成功')
    {
        echo "<script>
alert('{$msg}');
top.location='/sign'; 
</script>";
        exit ();
    }

    protected function validator($data, $rules, $messages = [])
    {

        $validator = Validator::make($data, $rules, $messages);

        if ($validator->fails()) {

            foreach ($validator->messages()->all() as $message) {
                $this->error($message);
            }
        }
        return $data;
    }


}
